<?php

namespace backend\modules\bigdata_bairong\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\modules\bigdata_bairong\models\BaiRongValidateLog;

/**
 * BaiRongValidateLogSearch represents the model behind the search form about `backend\modules\bigdata_bairong\models\BaiRongValidateLog`.
 */
class BaiRongValidateLogSearch extends BaiRongValidateLog
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'is_old_from_console'], 'integer'],
            [['strategy_type', 'req_name', 'req_idcard', 'req_mobile', 'api_send_post_data', 'resp_code', 'resp_swift_number', 'resp_data_for_api_send', 'created_at'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = BaiRongValidateLog::find()->orderBy(['id' => SORT_DESC]);

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'is_old_from_console' => $this->is_old_from_console,
        ]);

        $query->andFilterWhere(['like', 'strategy_type', $this->strategy_type])
            ->andFilterWhere(['like', 'req_name', $this->req_name])
            ->andFilterWhere(['like', 'req_idcard', $this->req_idcard])
            ->andFilterWhere(['like', 'req_mobile', $this->req_mobile])
            ->andFilterWhere(['like', 'api_send_post_data', $this->api_send_post_data])
            ->andFilterWhere(['like', 'resp_code', $this->resp_code])
            ->andFilterWhere(['like', 'resp_swift_number', $this->resp_swift_number])
            ->andFilterWhere(['like', 'resp_data_for_api_send', $this->resp_data_for_api_send]);

        if ( ! is_null($this->created_at) && strpos($this->created_at, ' - ') !== false ) {
            list($start_date, $end_date) = explode(' - ', $this->created_at);
            $query->andFilterWhere(['between', 'created_at', strtotime($start_date), strtotime($end_date.' 23:59:59')]);
        }

        return $dataProvider;
    }
}
